package krustyproject;

import javax.swing.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;

public class CookieGUI {

	private Database db;

	private JTabbedPane tabbedPane;

	public CookieGUI(Database db) {
		this.db = db;

		JFrame frame = new JFrame("Krusty Kookies");
		tabbedPane = new JTabbedPane();

		CookiePane userLoginPane = new CookiePane(db);
		tabbedPane.addTab("Create a new pallet", null, userLoginPane,
				"Create Pallets");

		PalletPane bookingPane = new PalletPane(db);
		tabbedPane.addTab("Pallet Handling", null, bookingPane,
				"Pallet Handling");
		
		BlockedPalletPane blockedpalletpane = new BlockedPalletPane(db);
		tabbedPane.addTab("Blocked pallets", null, blockedpalletpane,
				"Blocked pallets");

		SearchPane searchPane = new SearchPane(db);
		tabbedPane.addTab("Search by dates", null, searchPane,
				"Search");
		tabbedPane.setSelectedIndex(0);

		frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);

		tabbedPane.addChangeListener(new ChangeHandler());
		frame.addWindowListener(new WindowHandler());

		frame.setSize(500, 800);
		frame.setVisible(true);
		

		userLoginPane.displayMessage("Connecting to database ...");

		if (db.openConnection("db03", "hejhej")) {
			userLoginPane.displayMessage("Connected to database");
		} else {
			userLoginPane.displayMessage("Could not connect to database");
		}
	}

	class ChangeHandler implements ChangeListener {

		public void stateChanged(ChangeEvent e) {
			BasicPane selectedPane = (BasicPane) tabbedPane
					.getSelectedComponent();
			selectedPane.entryActions();
		}
	}

	class WindowHandler extends WindowAdapter {

		public void windowClosing(WindowEvent e) {
			db.closeConnection();
			System.exit(0);
		}
	}
}
